*********************************************************************************
*							Estimate Bunching Elasticity				*
*							Jennifer Mayo									*
*							Date: 17/04/2020									*
*********************************************************************************


clear all
set more off

global name "jmp4k"
global home "/Users/$name/Library/CloudStorage/OneDrive-UniversityofMissouri/Projects/Navigating the notches/"
global data "$home/Replication"
global output "$home/CN Output"


use "$data/Overall score.dta", clear

*********************************************************************************
*	Step 1: Create variables required in this do file 							*
*********************************************************************************


gen near4star = inrange(score,85,95)
	la var near4star "Overall score between 85 and 95"
	
*********************************************************************************
*	Step 2: Collapse Data into counts of firms by score bins					*
*********************************************************************************

	drop if PublicationYear==2011  
	drop if PublicationYear==2016  // drop years method changed
	duplicates drop

	egen double scoregroups = cut(score), at(0 (0.2) 100)	
		
*********************************************************************************
*	Step 3: Define Programs to Estimate Bunching and Bootstrap SE				*
*********************************************************************************
	
	* Define Bunching Estimate Program: 

	do "$data/bunchest.do" // Create bunching estimator program

	* Define Bootstrap Estimate Program: 
	
	cap program drop bunchestse
	program define bunchestse, rclass
		args ub threshold
		
		preserve
		
		bsample, cluster(ein)
		bunchest `ub' `threshold'
		

		return scalar lb_est = r(lb_est)
		return scalar excess_est = r(excess_est)
		return scalar b_av_est = r(b_av_est)		
		return scalar cflb_est = r(cflb)
		return scalar cfub_est = r(cfub)
		return scalar cfnotch_est = r(cfnotch)		
		
	restore
	end
	

*********************************************************************************
*	Step 4: Estimate Bunching on all charities, 4* threshold, all years/annual 		*
********************************************************************************* 

	* Restrict sample: 

	preserve
	drop if score<83
//	keep if PublicationYear == 2015
	
	count
	local n_1 = r(N)
	file open myfile using "$output/overall_bunching_estimates_92obstotal.tex", write text replace 
	file write myfile "`n_1'\unskip" 
	file close myfile
	
	bunchest 92 90  // args: ub and threshold
	mat res1 = (r(lb_est),r(excess_est),r(b_av_est))
	mat res2 = (r(cflb_est),r(cfub_est),r(cfnotch_est))

	matlist res1
	matlist res2
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if score<83
//	keep if PublicationYear == 2015	
	
	simulate lb=r(lb_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 92 90
	
	bstat, stat(res1) n(`n_1')
	
	mat A_1 = r(table)
	
	restore
	
* 2019	
	
	preserve
	duplicates drop
	drop if score<83
	keep if PublicationYear == 2019
	
	count
	local n_2 = r(N)
	file open myfile using "$output/overall_bunching_estimates_92obs2019.tex", write text replace 
	file write myfile "`n_2'\unskip" 
	file close myfile	
	
	bunchest 92 90  // args: ub and threshold
	mat res2 = (r(lb_est),r(excess_est),r(b_av_est))
	
	matlist res2
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if score<83
	keep if PublicationYear == 2019	
	
	simulate lb=r(lb_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 92 90
	
	bstat, stat(res2) n(`n_1')
	
	mat A_2 = r(table)
	
	restore
	
* 2018	
	
	preserve
	duplicates drop
	drop if score<83
	keep if PublicationYear == 2018
	
	count
	local n_3 = r(N)
	file open myfile using "$output/overall_bunching_estimates_92obs2018.tex", write text replace 
	file write myfile "`n_3'\unskip" 
	file close myfile
	
	bunchest 92 90  // args: ub and threshold
	mat res3 = (r(lb_est),r(excess_est),r(b_av_est))
	
	matlist res3
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if score<83
	keep if PublicationYear == 2018	
	
	simulate lb=r(lb_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 92 90
	
	bstat, stat(res3) n(`n_1')
	
	mat A_3 = r(table)
	
	restore

* 2017	
	
	preserve
	duplicates drop
	drop if score<83
	keep if PublicationYear == 2017
	
	count
	local n_4 = r(N)
	file open myfile using "$output/overall_bunching_estimates_92obs2017.tex", write text replace 
	file write myfile "`n_4'\unskip" 
	file close myfile
	
	bunchest 92 90  // args: ub and threshold
	mat res4 = (r(lb_est),r(excess_est),r(b_av_est))
	
	matlist res4
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if score<83
	keep if PublicationYear == 2017	
	
	simulate lb=r(lb_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 92 90
	
	bstat, stat(res4) n(`n_1')
	
	mat A_4 = r(table)
	
	restore

	
* 2015	
	
	preserve
	duplicates drop
	drop if score<83
	keep if PublicationYear == 2015
	
	count
	local n_5 = r(N)
	file open myfile using "$output/overall_bunching_estimates_92obs2015.tex", write text replace 
	file write myfile "`n_5'\unskip" 
	file close myfile
	
	bunchest 92 90  // args: ub and threshold
	mat res5 = (r(lb_est),r(excess_est),r(b_av_est))
	
	matlist res5
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if score<83
	keep if PublicationYear == 2015	
	
	simulate lb=r(lb_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 92 90
	
	bstat, stat(res5) n(`n_1')
	
	mat A_5 = r(table)
	
	restore	
		


*********************************************************************************
* Step 7: Create LateX Table for Paper												*
*********************************************************************************

	*All years pt. estimates: 
	
	mat r1 =  [round(A_1[1,3],.001)]
	
	* 2019 pt. estimates: 
	
	mat r2 =  [round(A_2[1,3],.001)]
	
	* 2018 pt. estimates: 
	
	mat r3 =  [round(A_3[1,3],.001)]
	
	* 2017 pt. estimates: 
	
	mat r4 =  [round(A_4[1,3],.001)]
	
	* 2015 pt. estimates: 
	
	mat r5 =  [round(A_5[1,3],.001)]
	
	* All years standard errors: 
	
	mat r11 =  [round(A_1[2,3],.001)]

	* 2019 standard errors: 
	
	mat r12 =  [round(A_2[2,3],.001)]
	
	* 2018 standard errors: 
	
	mat r13 =  [round(A_3[2,3],.001)]
	
	* 2017 standard errors: 
	
	mat r14 =  [round(A_4[2,3],.001)]
	
	* 2015 standard errors: 
	
	mat r15 =  [round(A_5[2,3],.001)]


	estadd matrix r1, replace
	estadd matrix r2, replace
	estadd matrix r3, replace
	estadd matrix r4, replace
	estadd matrix r5, replace
	estadd matrix r11, replace
	estadd matrix r12, replace
	estadd matrix r13, replace
	estadd matrix r14, replace
	estadd matrix r15, replace
	
	esttab using "$output/overall_bunching_estimates_92.tex", ///
		cells("r1 r2 r3 r4 r5" "r11(par) r12(par) r13(par) r14(par) r15(par)") collabels("All Years" "2019" "2018" "2017" "2015") ///
 		coeflabel(c1 "Average bunching response" )   nomtitles ///
		nonumbers gaps booktab frag plain replace


	
	
